/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:36 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class BaikiemtratracnghiemBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Baikiemtratracnghiem Này";
		private static string EX_FK_ITHONGTINBAIKIEMTRAID_INVALID="FK_iThongtinbaikiemtraID không hợp lệ";
		private static string EX_FK_IPHUONGANID_INVALID="FK_iPhuonganID không hợp lệ";
		private static string EX_ID_INVALID="FK_iThongtinbaikiemtraID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Baikiemtratracnghiem Theo ID
        /// </summary>
        /// <param name="FK_iThongtinbaikiemtraID">Int64:Baikiemtratracnghiem ID</param>
        /// <returns>BaikiemtratracnghiemEntity</returns>        
        public static BaikiemtratracnghiemEntity GetOne(Int64 FK_iThongtinbaikiemtraID)
        {
            
			if(FK_iThongtinbaikiemtraID<=0)
				throw new Exception(EX_ID_INVALID);
            return BaikiemtratracnghiemDAL.GetOne(FK_iThongtinbaikiemtraID);
        }
        /// <summary>
        /// Lấy về List các Baikiemtratracnghiem
        /// </summary>
        /// <returns>List BaikiemtratracnghiemEntity:List Baikiemtratracnghiem Cần lấy</returns>
        public static List<BaikiemtratracnghiemEntity> GetAll()
        {
            return BaikiemtratracnghiemDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Baikiemtratracnghiem
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Baikiemtratracnghiem Mới Thêm Vào</returns>
        public static Int32 Add(BaikiemtratracnghiemEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return BaikiemtratracnghiemDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Baikiemtratracnghiem
        /// </summary>
        /// <param name="entity">BaikiemtratracnghiemEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(BaikiemtratracnghiemEntity entity)
        {
            checkExist(entity.FK_iThongtinbaikiemtraID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return BaikiemtratracnghiemDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Baikiemtratracnghiem
        /// </summary>
        /// <param name="FK_iThongtinbaikiemtraID">Int64 : FK_iThongtinbaikiemtraID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 FK_iThongtinbaikiemtraID)
        {
            checkExist(FK_iThongtinbaikiemtraID);
            return BaikiemtratracnghiemDAL.Remove(FK_iThongtinbaikiemtraID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 FK_iThongtinbaikiemtraID)
        {
            BaikiemtratracnghiemEntity oBaikiemtratracnghiem=BaikiemtratracnghiemDAL.GetOne(FK_iThongtinbaikiemtraID);
            if(oBaikiemtratracnghiem==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">BaikiemtratracnghiemEntity: entity</param>
        private static void checkLogic(BaikiemtratracnghiemEntity entity)
        {   
			if (entity.FK_iThongtinbaikiemtraID < 0)
				throw new Exception(EX_FK_ITHONGTINBAIKIEMTRAID_INVALID);
			if (entity.FK_iPhuonganID < 0)
				throw new Exception(EX_FK_IPHUONGANID_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">BaikiemtratracnghiemEntity: BaikiemtratracnghiemEntity</param>
        private static void checkDuplicate(BaikiemtratracnghiemEntity entity,bool checkPK)
        {
            /* 
            Example
            List<BaikiemtratracnghiemEntity> list = BaikiemtratracnghiemDAL.GetAll();
            if (list.Exists(
                delegate(BaikiemtratracnghiemEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iThongtinbaikiemtraID != entity.FK_iThongtinbaikiemtraID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">BaikiemtratracnghiemEntity:entity</param>
        private static void checkFK(BaikiemtratracnghiemEntity entity)
        {            
        }
        #endregion
    }
}
